package com.example.jdimust.upload;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.jdimust.util.ACache;
import com.example.jdimust.register.NetworkRequest;
import com.example.jdimust.R;
import com.example.jdimust.login.LoginActivity;

import java.io.IOException;

import okhttp3.MediaType;

/**
 * Author: MaZongyang
 * Date: 2021/5/21/0021 20:12
 * Description:
 */
public class UploadAddValueActivity extends AppCompatActivity {
    private TextView addledname,addaccname;
    private EditText addkeyvalue,addvalue;
    private Button bt_add;
    private Spinner addsp;
    public static UploadAddValueActivity instance;
    private ACache acache;//缓存框架

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectCustomSlowCalls() //API等级11，使用StrictMode.noteSlowCode 自定义的耗时调用
                .detectDiskReads() //磁盘读取操作
                .detectDiskWrites() //磁盘写入操作
                .detectNetwork()  //网络操作
                //  .penaltyDialog() //弹出违规提示对话框
                .detectAll()
                .penaltyLog() //在Logcat 中打印违规异常信息
                .build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .detectActivityLeaks()  //Activity泄露
                .detectLeakedSqlLiteObjects()  //泄露的Sqlite对象
                .setClassInstanceLimit(this.getClass(), 1) //检测实例数量
                .penaltyLog()
                .build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addvalue);
        instance=this;
        System.out.println("已经进入activity_addvalue");
        acache=ACache.get(this);//创建ACache组件
        addledname = (TextView) findViewById(R.id.cre_ledname);
        addaccname = (TextView) findViewById(R.id.add_accname);
        addsp = (Spinner) findViewById(R.id.cre_sp_datatype);
        addkeyvalue = (EditText) findViewById(R.id.add_keyvalue);
        addvalue = (EditText) findViewById(R.id.add_value);
        bt_add = (Button) findViewById(R.id.bt_adddata);
        addledname.setText(acache.getAsString("acaledgerName"));
        addaccname.setText(acache.getAsString("acaaccountName"));
        bt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String addjson = "{\n" +
                        " \"ledgerHash\": \""+ acache.getAsString("acaledgerHash") +"\"," +
                        "\"addValue\": [\n" +
                        " {\n" +
                        " \"accountHash\": \""+ acache.getAsString("acaaccountHash") + "\",\n" +
                        " \"type\": \""+ addsp.getSelectedItem().toString() +"\",\n" +
                        " \"key\": \""+ addkeyvalue.getText().toString() +"\",\n" +
                        " \"value\":\"" + addvalue.getText().toString()+"\"\n" +
                        " }\n" +
                        " ]\n" +
                        " }";
                System.out.println("------------------------------\n"+ addjson + "\n-------------------------------------");
                MediaType header = MediaType.parse("application/json;charset=utf-8");
                String rejson = null;
                try {
                    rejson = NetworkRequest.postaccount("/Upload", addjson, header);
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                System.out.println(rejson);
//                Gson gson5 = new Gson();
//                UploadBean uploadbean = gson5.fromJson(rejson,UploadBean.class);

                Toast.makeText(UploadAddValueActivity.this, rejson, Toast.LENGTH_SHORT).show();
                showPopup("提示", rejson,"返回");
//                Intent intent = new Intent(UploadAddValueActivity.this, ValueActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面
                finish();
//                UploadAddValueActivity.instance.finish();

//                startActivity(intent);
//                AlertDialog alert = new AlertDialog.Builder(UploadAddValueactivity.this).create();
////               alert.setIcon(R.drawable.stop);
//                alert.setTitle("提示");
//                alert.setMessage(uploadbean.getMessage());
//                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "返回", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
////                  Intent intent = new Intent(UpdateActivity.this, DashboardFragment.class);
////                      startActivity(intent);
//                        Intent intent = new Intent(UploadAddValueactivity.this, LoginActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面
//                        startActivity(intent);
//                    }
//                });
//                alert.show();
            }
        });
    }

    public void showPopup(String show_title, String show_message, String show_button) {
        new AlertDialog.Builder(this)
                .setTitle(show_title)
                .setMessage(show_message)
                .setPositiveButton(show_button, null)
                .show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
    /**
     *菜单的点击事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.chageuser:
                Toast.makeText(this, "正在切换用户！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(UploadAddValueActivity.this, LoginActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面
                startActivity(intent);
                finish();
                break;
            default:
                break;
        }

        return true;
    }
}